package org.example;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

public class SetDemo {
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        try (Jedis jedis = jedisPool.getResource()) {
            //清空数据库
            jedis.flushAll();
            //测试代码如下
            //testSaddAndSmembers(jedis);
            //testSremAndSismember(jedis);
            //testScard(jedis);
            //testSinter(jedis);
            //testSunion(jedis);
            //testSdiff(jedis);
        }
    }
    private static void testSaddAndSmembers(Jedis jedis) {
        System.out.println("测试 sadd 和 smembers");
        jedis.sadd("key", "aaa", "bbb", "ccc");
        Set<String> members = jedis.smembers("key");
        System.out.println(members);
    }
    private static void testSremAndSismember(Jedis jedis) {
        System.out.println("测试 srem 和 sismember");
        jedis.sadd("key", "aaa", "bbb", "ccc");
        boolean ok = jedis.sismember("key", "aaa");
        System.out.println(ok);
        long n = jedis.srem("key", "aaa", "bbb");
        System.out.println(n);
        ok = jedis.sismember("key", "aaa");
        System.out.println(ok);
    }
    private static void testScard(Jedis jedis) {
        System.out.println("测试 scard");
        jedis.sadd("key", "aaa", "bbb", "ccc");
        long n = jedis.scard("key");
        System.out.println(n);
    }
    private static void testSinter(Jedis jedis) {
        System.out.println("测试 sinter");
        jedis.sadd("key1", "aaa", "bbb", "ccc");
        jedis.sadd("key2", "aaa", "bbb", "ddd");
        Set<String> results = jedis.sinter("key1", "key2");
        System.out.println(results);
    }
    private static void testSunion(Jedis jedis) {
        System.out.println("测试 sunion");
        jedis.sadd("key1", "aaa", "bbb", "ccc");
        jedis.sadd("key2", "aaa", "bbb", "ddd");
        Set<String> results = jedis.sunion("key1", "key2");
        System.out.println(results);
    }
    private static void testSdiff(Jedis jedis) {
        System.out.println("测试 sdiff");
        jedis.sadd("key1", "aaa", "bbb", "ccc");
        jedis.sadd("key2", "aaa", "bbb", "ddd");
        Set<String> results = jedis.sdiff("key1", "key2");
        System.out.println(results);
    }
}
